import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Scores extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1478970132293075796L;

	private static final int FONT_SIZE = 15;

	private File scoreFile;
	private TreeMap<String, Integer> sortedMap;
	private boolean displayScores = false;

	public Scores(File f) {
		scoreFile = f;
		sortedMap = new TreeMap<String, Integer>();
	}

	public void populateList() {
		if (scoreFile.exists()) {
			FileReader fstream;
			try {
				fstream = new FileReader(scoreFile.getPath());
				BufferedReader in = new BufferedReader(fstream);

				String line = null;
				while ((line = in.readLine()) != null && line.length() > 0) {
					// Scores should be of the form Name:Score
					String[] exploded = line.split(":");
					if (exploded.length < 1)
						throw new IllegalArgumentException(
								"Bad formatting in score file");

					sortedMap.put(exploded[0], Integer.parseInt(exploded[1]));
				}
			} catch (Exception e) {
				if (e instanceof IOException
						|| e instanceof NumberFormatException) {
					
					JOptionPane.showMessageDialog(this,
							"Bad formatting in file... "
									+ "Creating a new file", "Error",
							JOptionPane.ERROR_MESSAGE);
					
					scoreFile.renameTo(new File("Scores.dat.old" + System.currentTimeMillis()));
				}
			}
		}
		// Sort the new list
		// sortList();
	}

	/*
	private void sortList() {
		List<String> mapKeys = new ArrayList<String>(scoreMap.keySet());
		List<Integer> mapValues = new ArrayList<Integer>(scoreMap.values());
		TreeSet<Integer> sortedSet = new TreeSet<Integer>();
		for (int i = 0; i < mapValues.size(); i++) {
			boolean res = sortedSet.add(mapValues.get(i));
		}
		Object[] sortedArray = sortedSet.toArray();
		int size = sortedArray.length;

		for (int i = 0; i < size; i++) {
			sortedMap.put(mapKeys
					.get(mapValues.indexOf(sortedArray[i])),
					(Integer) sortedArray[i]);
		}
	}
	*/
	
	public void displayScores(boolean b) {
		displayScores = b;
		if (b)
			repaint();
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (displayScores) {
			Set<String> ref = sortedMap.keySet();
			Iterator<String> it = ref.iterator();

			int ySep = 20;
			g.setFont(new Font("Helvetica", Font.PLAIN, FONT_SIZE));
			FontMetrics fm = g.getFontMetrics();

			while (it.hasNext()) {
				if (ySep > this.getParent().getHeight()) 
					// Resize
					this.getParent().setBounds(this.getParent().getX(), 
							this.getParent().getY(), this.getParent().getWidth(), 
							(int)(this.getParent().getHeight() * 1.5));
				String name = it.next();
				Integer score = sortedMap.get(name);

				g.drawString(name, (int) ((this.getWidth() / 4.0) - (fm
						.stringWidth(name) / 2.0)), ySep);
				g.drawString(score + "",
						(int) ((3.0 * this.getWidth() / 4.0) - (fm
								.stringWidth(score + "") / 2.0)), ySep);
				ySep += 50;
			}
		}
	}
}
